﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

using Newtonsoft.Json;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using zijian666.Converts;
using zijian666.Converts.Json;

namespace UnitTest
{
    [TestClass]
    public class UnitTest6
    {
        [TestInitialize]
        public void Before()
        {

        }

        [TestMethod]
        public void JsonStringSerializerTest()
        {
            JsonStringSerializer serializer = new JsonStringSerializer();
            var g = Guid.NewGuid();
            var x = serializer.ToString(new Guid[] { g });
            Console.WriteLine(x);
            Assert.AreEqual("[\"" + g.ToString() + "\"]", x);
        }

        [TestMethod]
        public void JsonStringSerializerToObjectTest()
        {
            JsonStringSerializer serializer = new JsonStringSerializer();
            var arr = new[] { 1, 2, 3, 4, 5 };
            var json1 = JsonConvert.SerializeObject(arr);
            dynamic arr1 = serializer.ToObject(json1);
            Assert.AreEqual(arr.Length, arr1.Length);
            for (int i = 0; i < arr.Length; i++)
            {
                Assert.IsTrue(arr[i] == (int)arr1[i]);
            }
            var dict = new { id = 1, name = "zijian666" };
            var json2 = JsonConvert.SerializeObject(dict);
            dynamic dict1 = serializer.ToObject(json2);
            Assert.IsTrue(dict.id == (int)dict1.id);
            Assert.IsTrue(dict.name == (string)dict1.name);
        }

        [TestMethod]
        public void JsonStringSerializerToObject2Test()
        {

            {
                var data = 1m;
                JsonStringSerializer serializer = new JsonStringSerializer();
                var json = serializer.ToString(data);
                var obj = serializer.ToObject(json);
                var x = serializer.ToString(obj);
                Assert.AreEqual(json, serializer.ToString(obj));
            }

            {
                var data = new { id = 1m, name = "zijian666" };
                JsonStringSerializer serializer = new JsonStringSerializer();
                var json = serializer.ToString(data);
                var obj = serializer.ToObject(json);
                var x = serializer.ToString(obj);
                Assert.AreEqual(json, serializer.ToString(obj));
            }


            {
                var data = new Dictionary<string, dynamic>
                {
                    ["1"] = new { id = 1m, name = "zijian666" },
                    ["2"] = new { id = 2, name = "blqw" }
                };
                JsonStringSerializer serializer = new JsonStringSerializer();


                var json = serializer.ToString(data);
                var obj = serializer.ToObject(json);
                var x = serializer.ToString(obj);
                Assert.AreEqual(json, serializer.ToString(obj));
            }

        }
    }
}
